/**
 * 
 */
package stc.skymobi.cli.command;

import java.util.Arrays;
import java.util.Comparator;

import stc.skymobi.cli.CliCommand;
import stc.skymobi.cli.CliContext;
import stc.skymobi.cli.CommandRepository;

/**
 * @author Marvin.Ma
 *
 */
public class HelpCommand implements CliCommand<CliContext> {

	public String execute(String[] args, CliContext ctx) throws Exception {
		final CommandRepository	repo = ctx.getCommandRepository();
		
		if ( args.length >= 1 ) {
			final CliCommand<?> cmd = repo.findCommandByAction(args[0]);
			
			if ( null == cmd ) {
				return	"failed: can not found command [" + args[0] + "]";
			}
			else {
				return	"command [" + args[0] + "]: " + cmd.getHelp();
			}
		}
		else {
			final StringBuilder sb = new StringBuilder();
			final CliCommand<? extends CliContext>[] cmds = repo.getCommands();
			Arrays.sort(cmds, new Comparator<CliCommand<? extends CliContext>>() {

				public int compare(CliCommand<? extends CliContext> cmd1,
						CliCommand<? extends CliContext> cmd2) {
					return cmd1.getAction().compareTo(cmd2.getAction());
				}} );
			
			for ( CliCommand<?> cmd : cmds ) {
				sb.append("[");
				sb.append(cmd.getAction());
				sb.append("]: ");
				sb.append(cmd.getHelp());
				sb.append("\r\n");
			}
			return sb.toString();
		}
	}

	public String getAction() {
		return "help";
	}

	public String getHelp() {
		return null;
	}

}
